package com.softeem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softeem.domain.Admin;
import com.softeem.domain.Travle;
import com.softeem.domain.User;
import com.softeem.domain.vo.CommentVO;
import com.softeem.mapper.AdminMapper;
import com.softeem.mapper.UserMapper;
import com.softeem.service.IAdminService;
import com.softeem.service.ITravleService;
import com.softeem.service.IUserService;
import com.softeem.service.impl.UserServiceImpl;
import com.softeem.utils.RandomCodeUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-07-01
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService service;
    @Autowired
    AdminMapper mapper;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    ITravleService travleService;
    @Autowired
    IUserService userService;


    @RequestMapping("/test")
    public String test(){
        System.out.println("测试");
        return "indexUser";
     }


    @RequestMapping("/adminLogin")
    public String adminLogin(@ModelAttribute("adAccount")String adAccount,
                             @ModelAttribute("adPassword")String adPassword){
        Admin admin = new Admin();
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("adAccount",adAccount);
        qw.eq("adPassword",adPassword);
        List<Admin> list = admin.selectList(qw);

        if (list.size()==0){
            System.out.println("登录失败");
            return "/adminLogin";
        }else {
            System.out.println("登录成功");
            return "/index";
        }

     }



    @RequestMapping("/codes2")
     public String codes(@ModelAttribute("codes")String codes){
        RandomCodeUtils randomCodeUtils = new RandomCodeUtils();

         String s1 = redisTemplate.opsForValue().get("codes");

         if (s1.equals(codes)){
             System.out.println("生成："+s1);
             System.out.println("输入："+codes + "成功");
             return "";
         }else {
             System.out.println("生成:"+s1);
             System.out.println("输入"+codes + "失败");
             return "";
         }

      }

    @RequestMapping("/findAllUser")
    public String findAllUser(Model model){
        User user = new User();
        List<User> users = user.selectAll();
        model.addAttribute("userList",users);



        return "allUserManager";
     }

     @RequestMapping("/findAllTra")
     public String findAllTra(Model model){
         Travle travle = new Travle();
         List<Travle> travleList = travle.selectAll();
         List<Travle> traList = new ArrayList<>();
         for (Travle list : travleList) {
             if (list.getTraDid()==1){
                 traList.add(list);
             }
         }


         model.addAttribute("traList",traList);


         return "allTraManager";
      }


      @RequestMapping("/findAllYouHui")
      public String findAllYouHui(Model model){
          Travle travle = new Travle();
          List<Travle> travleList = travle.selectAll();
          List<Travle> travleList1 = new ArrayList<>();
          for (Travle traList : travleList) {
              if (traList.getTraLid() == 1  && traList.getTraDid()==1) {
                  travleList1.add(traList);
              }
          }

          model.addAttribute("traList",travleList1);

          return "youhuiManager";
       }

       @RequestMapping("/findAllRemen")
       public String findAllRemen(Model model){
           Travle travle = new Travle();
           List<Travle> travleList = travle.selectAll();
           List<Travle> travleList1 = new ArrayList<>();
           for (Travle traList : travleList) {
               if (traList.getTraPid() == 1 && traList.getTraDid()==1) {
                   travleList1.add(traList);
               }
           }

           model.addAttribute("traList",travleList1);

           return "/remenManager";

        }

        @RequestMapping("/addTra")
        public String addTra(@ModelAttribute("traName")String traName,
                             @ModelAttribute("traTitle")String traTitle,
                             @ModelAttribute("traIntraduce")String traIntraduce,
                             @ModelAttribute("traStisficing")String traStisficing,
                             @ModelAttribute("traPrice")Double traPrice,
                             Model model){
            Travle travle = new Travle();
            travle.setTraName(traName);
            travle.setTraTitle(traTitle);
            travle.setTraIntraduce(traIntraduce);
            travle.setTraStisficing(traStisficing);
            travle.setTraPrice(traPrice);


           travleService.addTra(travle);

            findAllTra(model);

            return "/allTraManager";
         }

         @RequestMapping("/updateRemen")
         public String updateRemen(@ModelAttribute("traId")String traId,
                                   Model model){
            travleService.updateRemen(traId);

             Travle travle = new Travle();
             List<Travle> travleList = travle.selectAll();

             model.addAttribute("traList",travleList);


             return "/allTraManager";

          }

    @RequestMapping("/updateYouhui")
    public String updateYouhui(@ModelAttribute("traId")String traId,
                              Model model){
        travleService.updateYouhui(traId);

        Travle travle = new Travle();
        List<Travle> travleList = travle.selectAll();

        model.addAttribute("traList",travleList);


        return "/allTraManager";

    }

    @RequestMapping("/updateDelete")
    public String updateDelete(@ModelAttribute("traId")String traId,
                               Model model){
        travleService.updateDelete(traId);

        Travle travle = new Travle();
        List<Travle> travleList = travle.selectAll();

        model.addAttribute("traList",travleList);


        return "/allTraManager";

    }

    @RequestMapping("/renewYouhui")
    public String renewYouhui(@ModelAttribute("traId")String traId,
                               Model model){
        travleService.renewYouhui(traId);
        
        Travle travle = new Travle();
        List<Travle> travleList = travle.selectAll();
        List<Travle> traList = new ArrayList<>();
        for (Travle travle1 : travleList) {
            if (travle1.getTraLid()==1 && travle1.getTraDid()==1){
                traList.add(travle1);
            }
        }

        model.addAttribute("traList",traList);


        return "/youhuiManager";

    }

    @RequestMapping("/renewRemen")
    public String renewRemen(@ModelAttribute("traId")String traId,
                              Model model){
        travleService.renewRemen(traId);

        Travle travle = new Travle();
        List<Travle> travleList = travle.selectAll();
        List<Travle> traList = new ArrayList<>();
        for (Travle travle1 : travleList) {
            if (travle1.getTraPid()==1 && travle1.getTraDid()==1){
                traList.add(travle1);
            }
        }

        model.addAttribute("traList",traList);


        return "/remenManager";

    }

    @RequestMapping("/deleteUser")
    public String deleteUser(@ModelAttribute("uId")String uId,Model model){

        User user = new User();

        user.deleteById(uId);

        List<User> users = user.selectAll();

        model.addAttribute("userList",users);

        return "/allUserManager";

     }

     @RequestMapping("/addUser")
     public String addUser(@ModelAttribute("uAccount")String uAccount,
                              @ModelAttribute("uPassword")String uPassword,
                              @ModelAttribute("uName")String uName,
                              @ModelAttribute("uIntroduce")String uIntroduce,
                              @ModelAttribute("uEmail")String uEmail,
                              @ModelAttribute("uMale")String uMale,
                              Model model){

         User user = new User();
         user.setuAccount(uAccount);
         user.setuName(uName);
         user.setuPassword(uPassword);
         user.setuMale(uMale);
         user.setuEmail(uEmail);
         user.setuIntroduce(uIntroduce);

         userService.addUser(user);



         List<User> users = user.selectAll();

         model.addAttribute("userList",users);

         return "/allUserManager";
      }

      @RequestMapping("/findBin")
      public String findBin(Model model){

          Travle travle = new Travle();
          List<Travle> travleList = travle.selectAll();
          List<Travle> traList = new ArrayList<>();
          for (Travle tralist : travleList) {
              if (tralist.getTraDid()==0){
                  traList.add(tralist);
              }
          }

          model.addAttribute("traList",traList);

          return "/bin";
       }

       @RequestMapping("/recoverBin")
       public String recoverBin(@ModelAttribute("traId")String traId,Model model){
           Travle travle = new Travle();

           travleService.recoverBin(traId);

           findBin(model);

           return "/bin";
        }

        @RequestMapping("/findAllComment")
        public String findAllComment(Model model){
            Travle travle = new Travle();
            List<CommentVO> commentList = travleService.findAllComment();
            model.addAttribute("commentList",commentList);


            return "/allCommentManager";
         }

         @RequestMapping("/deleteComment")
         public String deleteComment(@ModelAttribute("pId")Integer pId,Model model){

            travleService.deleteComment(pId);

             Travle travle = new Travle();
             List<CommentVO> commentList = travleService.findAllComment();
             model.addAttribute("commentList",commentList);


             return "/allCommentManager";

          }

}

